export function commonParse(element) {
  const result = {
    ...element.businessObject,
    ...element.businessObject.$attrs
  }
  return formatJsonKeyValue(result)
}

export function formatJsonKeyValue(result) {
  // 移除flowable前缀，格式化数组
  for (const key in result) {
    if (key.indexOf('flowable:') === 0) {
      const newKey = key.replace('flowable:', '')
      result[newKey] = result[key]
      delete result[key]
    }
  }
  result = documentationParse(result)
  return result
}

export function documentationParse(obj) {
  if ('documentation' in obj) {
    let str = ''
    obj.documentation.forEach(item => {
      str += item.text
    })
    obj.documentation = str
  }
  return obj
}

export function conditionExpressionParse(obj) {
  if ('conditionExpression' in obj) {
    obj.conditionExpression = obj.conditionExpression.body
  }else if('extensionElements' in obj){
	  const extensionElements = obj.extensionElements.values
	  for(let key in extensionElements){
		  if(extensionElements[key].$type==='flowable:IsCalcuSelectRouter'){
		  		obj.choice = extensionElements[key].value  
		  }
	  }
	  
  }
  return obj
}

export function userTaskParse(obj,userTypeOption) {
  for (const key in obj) {
    if (key === 'candidateUsers') {
      obj.userType = 'candidateUsers'
      // obj[key] = obj[key]?.split(',') || []
    } else if (key === 'candidateGroups') {
      obj.userType = 'candidateGroups'
      // obj[key] = obj[key]?.split(',') || []
    } else if(key==='userType'){
		let val = obj[key]
		let checkVal = new Array();
		getCascaderVal(userTypeOption,checkVal,val);
		obj.cascaderVal = checkVal;
		
	}
  }
  return obj
}

function getCascaderVal(obj,array,val){
	for(let index in obj){
		if(Object.values(obj[index]).indexOf(val)>0){
			array.push(obj[index].value)
			return true;
		}else if(Object.keys(obj[index]).indexOf('children')>0){
			let temp = obj[index].value;
			array.push(temp);
			let result = getCascaderVal(obj[index].children,array,val)
			if(result){
				return result;
			}else{
				array.pop();
			}
		}
	}
	return false;
}
